//
//  ZLBurnAfterReadingImageView.m
//  ZLYoYoIM-ZLYoYoIM
//
//  Created by 赵磊 on 2021/1/21.
//

#import "ZLBurnAfterReadingImageView.h"
#import "UIImage+ZLIMCustom.h"

@interface ZLBurnAfterReadingImageView ()

@property (nonatomic,strong) NSTimer *timer;

@property (nonatomic,strong) UIImage *BARImage;

@property (nonatomic,weak) UIButton *timeItem;

@property (nonatomic,unsafe_unretained) CGRect startFrame;

@property (nonatomic,unsafe_unretained) int time;

@property (nonatomic,copy) void (^action)(void);

@end

@implementation ZLBurnAfterReadingImageView

/** 展示查看阅后即焚的临时查看器
 * @param view 参照的视图（将按照该视图进行放大缩小）
 * @param image 模糊的图片
 * @param hdImage 高清大图
 * @param time 允许查看的时间
 * @param action 消失后的事件
 */
+ (void)show:(UIView *)view Image:(UIImage *)image HDImage:(UIImage *)hdImage Time:(int)time DismissAction:(void(^)(void))action {
    if (time < 1) {
        return;
    }
    CGRect frame = [view.superview convertRect:view.frame toView:UIApplication.sharedApplication.delegate.window];
    ZLBurnAfterReadingImageView *imageView = [[ZLBurnAfterReadingImageView alloc] initWithFrame:frame];
    imageView.contentMode = UIViewContentModeScaleAspectFit;
    imageView.clipsToBounds = YES;
    imageView.startFrame = frame;
    imageView.BARImage = image;
    imageView.time = time;
    imageView.backgroundColor = UIColor.blackColor;
    imageView.image = image;
    imageView.action = action;
    [UIApplication.sharedApplication.delegate.window addSubview:imageView];
    __weak typeof(imageView)weakSelf = imageView;
    [UIView animateWithDuration:0.25 animations:^{
        weakSelf.frame = UIScreen.mainScreen.bounds;
    } completion:^(BOOL finished) {
        weakSelf.image = hdImage;
        weakSelf.timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:weakSelf selector:@selector(runAction) userInfo:nil repeats:YES];
        [weakSelf.timer fire];
        [weakSelf.timeItem setTitle:[NSString stringWithFormat:@"剩余:%ds",time] forState:UIControlStateNormal];
        [[NSRunLoop currentRunLoop] addTimer:weakSelf.timer forMode:NSRunLoopCommonModes];
    }];
}

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.userInteractionEnabled = YES;
        BOOL isBangDevice = NO;
        if (@available(iOS 11.0, *)) {
            isBangDevice = UIApplication.sharedApplication.delegate.window.safeAreaInsets.bottom;
        }
        CGFloat ZL_Navigation_Height = isBangDevice ? 84.0 : 64.0;
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(UIScreen.mainScreen.bounds.size.width - 110.0 - 15.0, ZL_Navigation_Height - 40.0, 110.0, 30.0)];
        view.backgroundColor = [UIColor colorWithRed:249/255.0 green:115/255.0 blue:132/255.0 alpha:1.0];
        view.layer.cornerRadius = CGRectGetHeight(view.frame) / 2.0;
        view.layer.masksToBounds = YES;
        [self addSubview:view];
        UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 70.0, 30.0)];
        button.titleLabel.font = [UIFont systemFontOfSize:13.0];
        [view addSubview:button];
        self.timeItem = button;
        button = [[UIButton alloc] initWithFrame:CGRectMake(70.0, 0, 40.0, 30.0)];
        [button setImage:[UIImage imageNamedInIM:@"guanbi"] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(dismiss) forControlEvents:UIControlEventTouchUpInside];
        [view addSubview:button];
        CALayer *layer = [CALayer layer];
        layer.frame = CGRectMake(70.0, 5.0, 0.8, 20.0);
        layer.backgroundColor = UIColor.whiteColor.CGColor;
        [view.layer addSublayer:layer];
    }
    return self;
}

- (void)runAction {
    if (self.time == 0) {
        [self dismiss];
        return;
    }
    [self.timeItem setTitle:[NSString stringWithFormat:@"剩余:%ds",self.time] forState:UIControlStateNormal];
    self.time -= 1;
}

- (void)dismiss {
    [self stopTimer];
    self.image = self.BARImage;
    __weak typeof(self)weakSelf = self;
    [UIView animateWithDuration:0.25 animations:^{
        weakSelf.frame = weakSelf.startFrame;
    } completion:^(BOOL finished) {
        if (self.action) {
            self.action();
        }
        [weakSelf removeFromSuperview];
    }];
}

- (void)stopTimer {
    if (self.timer) {
        [self.timer invalidate];
        self.timer = nil;
    }
}

@end
